<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 17-2-22
 * Time: 下午11:04
 */

namespace Ex;

use SebastianBergmann\CodeCoverage\Report\PHP;

if(true){
    class One{
        public function one(){
            echo "true".PHP_EOL;
        }

        public static function staticOne(){
            echo __METHOD__.PHP_EOL;
        }
    }
}else{
    class One{
        function one(){
            echo "false";
        }
    }
}


(new One())->one();

$one = 'Ex\One';
$one::staticOne();


call_user_func(['Ex\One','staticOne']);
call_user_func('Ex\One::staticOne');



//单例模式

class Singleton{

    private static $instance = null;

    private function __construct(){}
    private function __clone(){}

    public static function getInstance(){
        if (!is_null(self::$instance) && self::$instance instanceof self){
            return self::$instance;
        }else{
            return new self();
        }
    }

    public function test(){
        echo "success".PHP_EOL;
    }
}

$singleton = Singleton::getInstance();
$singleton2 = Singleton::getInstance();
$singleton3 = $singleton2;
if($singleton === $singleton2){
    echo "this is 单例模式";
}else{
    echo "no Singleton";
}

if($singleton2 === $singleton3){
    echo "===";
}

$singleton->test();
$singleton2->test();



class stdObject {
    public function __construct(array $arguments = array()) {
        if (!empty($arguments)) {
            foreach ($arguments as $property => $argument) {
                $this->{$property} = $argument;
            }
        }
    }
    public function __call($method, $arguments) {
        $arguments = array_merge(
            array("stdObject" => $this),
            $arguments); // Note: method argument 0 will always referred to the main class ($this).
        if (isset($this->{$method}) && is_callable($this->{$method})) {
            return call_user_func_array($this->{$method}, $arguments);
        } else {
            throw new \Exception("Fatal error: Call to undefined method stdObject::{$method}()");
        }
    }
}

$stdObject = new stdObject(['id' => 1]);
$stdObject->name = "zing";
echo $stdObject->name;
echo $stdObject->id;

$stdObject->getName = function (){return "wocao";};
echo $stdObject->getName();
$stdObject2 = new stdObject(['getID' => function(){return 10000;}]);

echo $stdObject2->getID();


$stdObject->getInfo = function ($stdObject){
    echo PHP_EOL.$stdObject->name.PHP_EOL.$stdObject->id.PHP_EOL;
};
$stdObject->getInfo();



/////////////////////////////////////////////////////////////////////////////
class _
{
    public function __construct( array $cfg){
        foreach($cfg as $k=>$v){
            $this->{$k}=$v;
        }
    }

    public function __call( $fn, array $args){
        if(isset($this->{$fn})){
            array_unshift($args, $this);
            call_user_func_array( $this->{$fn}, $args);
        }
    }
}

$o = new _([
    "color"=>"red",
    "run"=>function(){
        echo "My color is : ";
    }]);

$o->run();



$instance = new \stdClass();

$assigned   =  $instance;
$assigned2 = &$assigned;
$assigned3 = clone $assigned2;
$reference  = & $instance;

$instance->var = '$assigned will have this value';

$instance = null; // $instance and $reference become null
var_dump($assigned === $assigned2);
var_dump($assigned3 === $assigned2);//false
var_dump($assigned3 == $assigned2);//false
print_r($instance);
print_r($reference);
print_r($assigned);